package com.chen.common.nacos;

import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.*;

/**
 * 跨命名空间获取nacos中的配置
 * @author ChenJinLong
 */
@Slf4j
@Deprecated
@Order(Integer.MIN_VALUE + 200)
public class NacosPropertySourceConfig implements EnvironmentPostProcessor {

    private static Boolean isLoad = false;

    @Override
    @SneakyThrows
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        // 避免EnvironmentPostProcessor执行多次的问题
        if (NacosPropertySourceConfig.isLoad){
            return;
        }
        NacosPropertySourceConfig.isLoad = true;

        String host = environment.getProperty("spring.cloud.nacos.config.server-addr", "127.0.0.1:8848");
        String username = environment.getProperty("spring.cloud.nacos.username", "nacos");
        String password = environment.getProperty("spring.cloud.nacos.password", "nacos");
        NacosParam param = new NacosParam();
        param.setHost(host);
        param.setUsername(username);
        param.setPassword(password);
        param.setNamespace(null);
        param.setGroup("DEFAULT_GROUP");

        Map<String, Object> configMap = new HashMap<>();
        List<String> configList = ergodicConfig(environment, "nacos-properties-configs");
        for (String dataId : configList) {
            dataId += ".properties";
            param.setDataId(dataId);
            Properties properties = NacosConfigUtil.getNacosConfig(param);
            if (properties != null){
                System.out.println("加载nacos配置 -> " + dataId);
                for (Object key : properties.keySet()) {
                    configMap.put(key.toString(), properties.get(key));
                }
            }
        }
        environment.getPropertySources().addLast(new MapPropertySource("nacosPropertiesConfig", configMap));
    }

    private List<String> ergodicConfig(ConfigurableEnvironment environment, String key){
        // 遍历加载配置
        int index = 0;
        List<String> configList = new ArrayList<>();
        while (index != -1){
            String value = environment.getProperty(StrUtil.format("{}[{}]", key, index));
            if (StrUtil.isNotBlank(value)){
                ++index;
                configList.add(value);
            }else {
                index = -1;
            }
        }
        String configValue = environment.getProperty(key);
        if (StrUtil.isNotBlank(configValue)){
            if (configValue.contains(",")){
                String[] cons = configValue.split(",");
                configList.addAll(Arrays.asList(cons));
            }else {
                configList.add(configValue);
            }
        }

        return configList;
    }
}
